//------------------------------------------------------------------------------
//  <copyright file="HeartbeatReceiver.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------
namespace EV4Test
{
    using System;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Hardware.EV4;
    using Microsoft.Robotics.Hardware.Runtime.DAC;
    using Microsoft.Robotics.Runtime;
    
    /// <summary>
    /// Receives output from the HeartbeatAgent
    /// </summary>
    [DataContract]
    public class HeartbeatReceiver :
        Agent,
        ISubscriptionReceiver<AccelerometerMessage>,
        ISubscriptionReceiver<BatteryMessage>,
        ISubscriptionReceiver<ButtonMessage>,
        ISubscriptionReceiver<CompassMessage>,
        ISubscriptionReceiver<DifferentialDriveMessage>,
        ISubscriptionReceiver<EncoderMessage>,
        ISubscriptionReceiver<FanMessage>,
        ISubscriptionReceiver<GyroscopeMessage>,
        ISubscriptionReceiver<HumidityMessage>,
        ISubscriptionReceiver<IRProximityMessage>,
        ISubscriptionReceiver<JointMessage>,
        ISubscriptionReceiver<PowerMessage>,
        ISubscriptionReceiver<ProjectorMessage>,
        ISubscriptionReceiver<SonarMessage>,
        ISubscriptionReceiver<TemperatureMessage>
    {
        /// <summary>
        /// The agent we are subscribing to.
        /// </summary>
        private IAgentHost producer;

        /// <summary>
        /// Initializes a new instance of the HeartbeatReceiver class.
        /// </summary>
        public HeartbeatReceiver()
        {
        }

        /// <summary>
        /// Finds and subscribes to the publishing agent.
        /// </summary>
        /// <param name="locator">A locator to use.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            this.producer = locator.GetAgent("HeartbeatAgent");
            this.producer.Subscribe<AccelerometerMessage>(this);
            this.producer.Subscribe<BatteryMessage>(this);
            this.producer.Subscribe<ButtonMessage>(this);
            this.producer.Subscribe<CompassMessage>(this);
            this.producer.Subscribe<DifferentialDriveMessage>(this);
            this.producer.Subscribe<EncoderMessage>(this);
            this.producer.Subscribe<FanMessage>(this);
            this.producer.Subscribe<GyroscopeMessage>(this);
            this.producer.Subscribe<HumidityMessage>(this);
            this.producer.Subscribe<IRProximityMessage>(this);
            this.producer.Subscribe<JointMessage>(this);
            this.producer.Subscribe<PowerMessage>(this);
            this.producer.Subscribe<ProjectorMessage>(this);
            this.producer.Subscribe<SonarMessage>(this);
            this.producer.Subscribe<TemperatureMessage>(this);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(AccelerometerMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(BatteryMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(ButtonMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(CompassMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(DifferentialDriveMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(EncoderMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(FanMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(GyroscopeMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(HumidityMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(IRProximityMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(JointMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(PowerMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(ProjectorMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(SonarMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(TemperatureMessage message)
        {
            Console.Write(message.State);
        }

        /// <summary>
        /// Activates upstream agents.
        /// </summary>
        public override void OnActivated()
        {
            this.producer.Activate();
        }
    }
}
